package queryProccessor;
import java.util.ArrayList;
import java.util.StringTokenizer;


public class Insert extends Command {
	protected String[] columns;
	protected String[] values;

	private ArrayList<String> finalArray;
	private final String[] ideal1 = { "INSERT INTO", "table_name", "columns",
			"VALUES", "values" };
	private final String[] ideal2 = { "INSERT INTO", "table_name", "VALUES",
			"values" };
	boolean result = true;

	public String[] getColumns() {
		return columns;
	}

	public void setColumns(String[] columns) {
		this.columns = columns;
	}

	public String[] getValues() {
		return values;
	}

	public void setValues(String[] values) {
		this.values = values;
	}

	public Insert(String s) {
		insertStatment = s;
		finalArray = new ArrayList<String>();
		try {
			convertToArray();
			result = checkSyntax();
			System.out.println(result);
		} catch (Exception e) {
			System.out.println("wrong syntax");
		}
	}

	@Override
	public void convertToArray() {
		int in = search("INSERT INTO ", insertStatment);
		int v = search("VALUES ", insertStatment);
		int sc = search(";", insertStatment);
		if (in != -1 && v != -1 && sc != -1) {
			finalArray.add("INSERT INTO");
			String s = insertStatment.substring(in + 11, v);
			s = s.trim();
			int l = search("(", s);
			if (l == -1) {
				finalArray.add(s);
			} else {
				finalArray.add(s.substring(0, l).trim());
				finalArray.add(s.substring(l).trim());
			}
			finalArray.add("VALUES");
			s = insertStatment.substring(v + 6, sc);
			s = s.trim();
			finalArray.add(s);
		}
	}

	public boolean lools(String[] ideal, int[] no) {
		boolean result = true;
		for (int i = 0; i < finalArray.size(); i++) {
			if (!isTrue(i, no)) {
				if (!finalArray.get(i).equalsIgnoreCase(ideal[i])) {
					result = false;
					break;
				}
			}
		}
		return result;
	}

	private boolean isTrue(int x, int[] no) {
		for (int i = 0; i < no.length; i++) {
			if (no[i] == x)
				return true;
		}
		return false;
	}

	public boolean checkSyntax() {
		boolean result = true;
		if (finalArray.size() == 5) {
			int[] no = { 1, 2, 4 };
			if (result == lools(ideal1, no)) {
				table_name = finalArray.get(1);
				columns = divide(finalArray.get(2));
				values = divide(finalArray.get(4));
			}
		} else if (finalArray.size() == 4) {
			int[] no = { 1, 3 };
			if (result == lools(ideal2, no)) {
				table_name = finalArray.get(1);
				values = divide(finalArray.get(3));
				columns = new String[0];
			}
		} else
			result = false;
		return result;
	}

	private String[] divide(String s) {
		if (s.charAt(0) != '(' || s.charAt(s.length() - 1) != ')') {
			result = false;
		}
		s = s.substring(1, s.length() - 1);
		StringTokenizer str = new StringTokenizer(s, ",");
		String[] result = new String[str.countTokens()];
		int i = 0;
		while (str.hasMoreTokens()) {
			result[i] = str.nextToken().trim();
			i++;
		}
		return result;
	}
	


	
}